﻿/**
 * @author Mif
 */
var setShape = {

    /**
     * 暂用用户拍照骨骼变形
     */
    temporaryPhotoSetShape: function (userData, shoulder, PresonXY, name, groupName, sex) {

        var shoulderScale = null; //肩宽比例
        var legHeightScale = null; //腿长比例
        var waistScaleW = null; //腰正宽比例
        var waistScaleH = null; //腰侧宽比例
        var hiplinScaleW = null; //臀正宽比例
        var hiplinScaleH = null; //臀侧宽比例
        var bustScaleW = null; //胸正宽比例
        var bustScaleH = null; //胸侧宽比例
        var ribScaleW = null; //肋正宽比例
        var ribScaleH = null; //肋侧宽比例
        var heightScale = null; //身高比例
        var footScale = null; //脚长比例
        var basicsHeight = null; //基础模特身高（不算头长）
        var basicsUpperScale = null; //基础模特上身比例
        var basicsDonwScale = null; //基础模特下身比例
        var oldUpperDonwScale = null; //用户照片中上下身比例
        var newRellyDonw = null; //用户照片下身

        var Person = PresonXY;
        var waistlineFlag = 1; //腰线flag        


        var origineXY = Person;//用户填入数据

        //肩宽与头长的最大比例和最小比例
        var shoulderMax = 250 * 2.2;//肩宽最大比例
        var shoulderMin = 250 * 1.8;//肩宽最小比例
        if (shoulder > shoulderMax) {
            shoulder = shoulderMax;
        } else if (shoulder < shoulderMin) {
            shoulder = shoulderMin;
        }

        if (sex == 0) {

            shoulderScale = shoulder / 383;

            waistScaleW = origineXY.waistlineXY.width / (267.35 * 1.06);
            waistScaleH = origineXY.waistlineXY.height / (195.83 * 1.06);

            hiplinScaleW = origineXY.hiplineXY.width / 304.11;
            hiplinScaleH = origineXY.hiplineXY.height / (207.95 * 1.04);

            bustScaleW = origineXY.chestXY.width / 265.09;
            bustScaleH = origineXY.chestXY.height / (226.23 * 1.1);

            if (userData.length > 1) {
                ribScaleW = origineXY.ribXY.width / 226.68;
                ribScaleH = origineXY.ribXY.height / (187.31 * 1.1);
            }


            heightScale = userData[0].shoulder / 1500//1278.9;

            basicsHeight = 1278.9;

            basicsUpperScale = (basicsHeight - 683.8) / basicsHeight;
            basicsDonwScale = 683.8 / basicsHeight;
            oldUpperDonwScale = basicsUpperScale / basicsDonwScale;

            newRellyDonw = (basicsHeight - 683.8) / oldUpperDonwScale;

            legHeightScale = (userData[0].crotch) / newRellyDonw;

            footScale = 230 / 225;

        } else {

            shoulderScale = shoulder / 412;

            //legHeightScale = (userData[0].crotch) / 794.6;

            waistScaleW = origineXY.waistlineXY.width / 300.27;
            waistScaleH = origineXY.waistlineXY.height / 203.75;

            hiplinScaleW = origineXY.hiplineXY.width / 327.62;
            hiplinScaleH = origineXY.hiplineXY.height / 234.91;

            bustScaleW = origineXY.chestXY.width / 308.78;
            bustScaleH = origineXY.chestXY.height / 223.85;

            heightScale = userData[0].shoulder / 1750//1411.4;

            basicsHeight = 1411.4;
            basicsUpperScale = (basicsHeight - 794.6) / basicsHeight;
            basicsDonwScale = 794.6 / basicsHeight;
            oldUpperDonwScale = basicsUpperScale / basicsDonwScale;

            newRellyDonw = (basicsHeight - 794.6) / oldUpperDonwScale;

            legHeightScale = (userData[0].crotch) / newRellyDonw;

            footScale = 250 / 277;

        }
        console.log(origineXY);
        console.log('胸侧宽', bustScaleH, origineXY.chestXY.height);
        console.log('胸正宽', bustScaleW, origineXY.chestXY.width);
        console.log('腰正宽', waistScaleW, origineXY.waistlineXY.width);
        console.log('腰侧宽', waistScaleH, origineXY.waistlineXY.height);
        console.log('臀正宽', hiplinScaleW, origineXY.hiplineXY.width);
        console.log('臀侧宽', hiplinScaleH, origineXY.hiplineXY.height);
        //三围侧宽优化
        if (bustScaleH < 0.86) {
            bustScaleH = 0.86;
        }
        if (waistScaleH < 0.86) {
            waistScaleH = 0.86;
        }
        if (hiplinScaleW < 0.86) {
            hiplinScaleW = 0.86;
        }
        //臀围优化
        if (hiplinScaleH > hiplinScaleW * 1.1) {
            hiplinScaleH = hiplinScaleW * 1.1;
        }
        //胸围优化
        if (bustScaleW > hiplinScaleW * 1.05) {
            bustScaleW = hiplinScaleW * 1.05;
        } else if (bustScaleW < hiplinScaleW * .96) {
            bustScaleW = hiplinScaleW * .96;
        }
        // if (bustScaleH > hiplinScaleH * 1.05) {
        //     bustScaleH = hiplinScaleH * 1.05;
        // }
        //肋围优化
        if (ribScaleW > bustScaleW * 1.05) {
            ribScaleW = bustScaleW * 1.05;
        } else if (ribScaleW < bustScaleW * .96) {
            ribScaleW = bustScaleW * .96;
        }
        if (ribScaleH > bustScaleH * 1.05) {
            //ribScaleH = bustScaleH * 1.05;
        } else if (ribScaleH < waistScaleW * .96) {
            ribScaleH = waistScaleW * .96;
        }
        //腰围优化
        if (waistScaleW > hiplinScaleW * 1.05) {
            waistScaleW = hiplinScaleW * 1.05;
        } else if (waistScaleW < hiplinScaleW * .96) {
            waistScaleW = hiplinScaleW * .96;
        }
        //腿长优化
        if (legHeightScale > 1.2) {
            legHeightScale = 1.2;
        } else if (legHeightScale < .9) {
            legHeightScale = .9;
        }
        //胸侧宽优化
        if (bustScaleH > waistScaleH * 1.02) {
            bustScaleH = waistScaleH * 1.02
        }
        //是否有腰线
        if (!Person.waistlineXY.height < Person.hiplineXY.width * 1.1) {
            waistlineFlag = 0;
        } else {
            waistlineFlag = 1;
        }
        console.log('腰正宽', waistScaleW, origineXY.waistlineXY.width);
        //console.log('肩宽', shoulderScale, '臀正宽', hiplinScaleW, '胸侧宽', bustScaleH, origineXY.chestXY.height, '腰正宽', waistScaleW, origineXY.waistlineXY.width, '腰侧宽', waistScaleH, origineXY.waistlineXY.height, '臀侧宽', hiplinScaleH, origineXY.chestXY.height, '臀正宽', hiplinScaleW, origineXY.chestXY.width, '胸正宽', bustScaleW, '肋正宽', ribScaleW, '肋侧宽', ribScaleH);
        var tmp = scene.getObjectByName(name);

        if (!sex == 0) {

            //var box = Object3D.getObjectByName('fbxGroup');
            //headBias = (legHeightScale - 1) * 794.6 * .1 - 2;

            //box.position.y = headBias;  //腿长改变后人体位置自适应
            if (heightScale > 1) {
                tmp.parent.scale.y = heightScale * .84;
            } else {
                tmp.parent.scale.y = heightScale * .94;
            }

            for (var i = 0; i < tmp.skeleton.bones.length; i++) {

                if (tmp.skeleton.bones[i].name === 'Trunk_Chest_S') {

                    tmp.skeleton.bones[i].scale.x = bustScaleW;
                    tmp.skeleton.bones[i].scale.x = bustScaleH;
                    tmp.skeleton.bones[i].scale.z = bustScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Hip') {//躯体

                    tmp.scale.x = heightScale;
                    tmp.scale.y = heightScale;
                    tmp.scale.z = heightScale;

                } else if (tmp.skeleton.bones[i].name === 'Trunk_Venter_S') {//腹部
                    var waistScaleH1 = null;
                    if (waistScaleH > 1.2) {
                        waistScaleH1 = waistScaleH * .85;
                    }
                    if (waistScaleH1 < 1) {
                        waistScaleH1 = 1;
                    }
                    console.log(waistScaleH);
                    tmp.skeleton.bones[i].scale.x = waistScaleW;
                    tmp.skeleton.bones[i].scale.z = waistScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Rib_S') {//肋部
                    tmp.skeleton.bones[i].scale.x = ribScaleW;
                    tmp.skeleton.bones[i].scale.z = ribScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Hip_S') { //腰部
                    tmp.skeleton.bones[i].scale.x = waistScaleW;
                    tmp.skeleton.bones[i].scale.z = waistScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Pevis_S') { //臀部
                    tmp.skeleton.bones[i].scale.x = hiplinScaleW;
                    var hiplinScaleH1 = 0;
                    if (hiplinScaleH > 1.1 && hiplinScaleH <= 1.19) {
                        hiplinScaleH1 = hiplinScaleH * .9;
                    } else if (hiplinScaleH > 1.19) {
                        hiplinScaleH1 = hiplinScaleH * .84;
                    } else {
                        hiplinScaleH1 = 1;
                    }
                    tmp.skeleton.bones[i].scale.y = hiplinScaleH1;
                    tmp.skeleton.bones[i].scale.z = hiplinScaleH1;
                } else if (tmp.skeleton.bones[i].name === 'L_Leg_Up' || tmp.skeleton.bones[i].name === 'R_Leg_Up') { //大腿部
                    tmp.skeleton.bones[i].scale.x = hiplinScaleW;
                    console.log(legHeightScale);
                    if (legHeightScale > 1.12) {
                        tmp.skeleton.bones[i].scale.y = legHeightScale * .94; //大腿长
                    }

                    tmp.skeleton.bones[i].scale.z = hiplinScaleW;
                } else if (tmp.skeleton.bones[i].name === 'Trunk6_S') {//肩部
                    var shoulderScale1 = 0;
                    if (shoulderScale > hiplinScaleW) {
                        shoulderScale = hiplinScaleW;
                    }
                    if (shoulderScale > 1.2) {
                        shoulderScale1 = 1.18;
                    } else if (shoulderScale <= 1.2 && shoulderScale > 1.1) {
                        shoulderScale1 = shoulderScale * 0.8;
                    } else {
                        shoulderScale1 = 1;
                    }
                    if (shoulderScale1 < 1) {
                        shoulderScale1 = 1;
                    }
                    tmp.skeleton.bones[i].scale.x = shoulderScale1;
                    //tmp.skeleton.bones[i].scale.z = shoulderScale1;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Clavicle_S') { //胸肩过度处
                    tmp.skeleton.bones[i].scale.x = bustScaleW;
                } else if (tmp.skeleton.bones[i].name === 'L_Foot' || tmp.skeleton.bones[i].name === 'R_Foot') {//脚部
                    //if (hiplinScaleW > 1.2) {
                    //    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * 0.91;
                    //    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * 0.91;
                    //    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * 0.91;
                    //} else if (hiplinScaleW <= 1.2 && hiplinScaleW > 1) {
                    //    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * 0.9;
                    //    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * 0.9;
                    //    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * 0.9;
                    //} else if (hiplinScaleW === 1) {
                    //    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * 0.89;
                    //    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * 0.89;
                    //    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * 0.89;
                    //} else {
                    //    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * .89;
                    //    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * .88;
                    //    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * .88;
                    //}
                    console.log(1 / hiplinScaleW * footScale);
                    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * footScale;
                    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * footScale;
                    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * footScale;
                } else if (tmp.skeleton.bones[i].name === 'L_Hand' || tmp.skeleton.bones[i].name === 'R_Hand') {//手部
                    if (hiplinScaleW > 1.2) {
                        tmp.skeleton.bones[i].scale.x = 1.003;
                        tmp.skeleton.bones[i].scale.y = 1.003;
                        tmp.skeleton.bones[i].scale.z = 1.003;
                    } else if (hiplinScaleW <= 1.2 && hiplinScaleW > 1) {
                        tmp.skeleton.bones[i].scale.x = 1.001;
                        tmp.skeleton.bones[i].scale.y = 1.001;
                        tmp.skeleton.bones[i].scale.z = 1.001;
                    } else if (hiplinScaleW === 1) {
                        tmp.skeleton.bones[i].scale.x = 1;
                        tmp.skeleton.bones[i].scale.y = 1;
                        tmp.skeleton.bones[i].scale.z = 1;
                    } else {
                        tmp.skeleton.bones[i].scale.x = .9;
                        tmp.skeleton.bones[i].scale.y = .9;
                        tmp.skeleton.bones[i].scale.z = .9;
                    }
                }
                if (tmp.skeleton.bones[i].parent.name === 'Trunk6_S') {
                    tmp.skeleton.bones[i].scale.x = 1 / shoulderScale;
                    tmp.skeleton.bones[i].scale.z = 1 / shoulderScale;
                    if (tmp.skeleton.bones[i].name === 'L_Arm_Shoulder' || tmp.skeleton.bones[i].name === 'R_Arm_Shoulder') {
                        var waistScaleW1 = 0;
                        if (waistScaleW > 1.1) {
                            waistScaleW1 = waistScaleW * .9;
                        } else {
                            waistScaleW1 = 1;
                        }
                        tmp.skeleton.bones[i].scale.x = waistScaleW1;
                        tmp.skeleton.bones[i].scale.y = legHeightScale;
                        tmp.skeleton.bones[i].scale.z = waistScaleW1;
                    }
                }
            }
        } else {
            //女模变形
            //var box = Object3D.getObjectByName('fbxGroup');
            //headBias = -15 + (legHeightScale - 1) * 683.8 * .1 - 2;

            //box.position.y = headBias;  //腿长改变后人体位置自适应
            if (heightScale > 1) {
                tmp.parent.scale.y = heightScale * .84;
            } else {
                tmp.parent.scale.y = heightScale * .94;
            }

            console.log(heightScale, tmp);
            for (var i = 0; i < tmp.skeleton.bones.length; i++) {
                if (tmp.skeleton.bones[i].name === 'Trunk_Chest_S') {//胸部
                    tmp.skeleton.bones[i].scale.x = bustScaleW;
                    tmp.skeleton.bones[i].scale.y = bustScaleH;
                    tmp.skeleton.bones[i].scale.z = bustScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Hip') {//躯体
                    console.log(2323);
                    tmp.scale.x = heightScale;
                    tmp.scale.y = heightScale;
                    tmp.scale.z = heightScale;
                } else if (tmp.skeleton.bones[i].name === 'L_Chest' || tmp.skeleton.bones[i].name === 'R_Chest') { //乳房
                    var bustScaleHBreast;
                    if (bustScaleH < 1) {
                        bustScaleHBreast = 1;
                    } else {
                        bustScaleHBreast = bustScaleH;
                    }
                    tmp.skeleton.bones[i].scale.x = bustScaleHBreast;
                    tmp.skeleton.bones[i].scale.y = bustScaleHBreast;
                    tmp.skeleton.bones[i].scale.z = bustScaleHBreast;
                } else if (tmp.skeleton.bones[i].name === 'L_Shoulder' || tmp.skeleton.bones[i].name === 'R_Shoulder') {//肩部
                    var shoulderScale1 = 0;
                    if (shoulderScale > hiplinScaleW) {
                        shoulderScale = hiplinScaleW;
                    }
                    if (shoulderScale > 1.2) {
                        shoulderScale1 = 1.18;
                    } else if (shoulderScale <= 1.2 && shoulderScale > 1.1) {
                        shoulderScale1 = shoulderScale * 0.9;
                    } else {
                        shoulderScale1 = 1;
                    }
                    if (shoulderScale1 < 1) {
                        shoulderScale1 = 1;
                    }
                    tmp.skeleton.bones[i].scale.y = shoulderScale1;
                    // tmp.skeleton.bones[i].scale.x = shoulderScale1;
                    // tmp.skeleton.bones[i].scale.z = shoulderScale1;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Rib_S') {//肋部
                    console.log(ribScaleH);
                    tmp.skeleton.bones[i].scale.x = ribScaleW;
                    tmp.skeleton.bones[i].scale.z = ribScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Trunk_Venter_S') {//腰部
                    tmp.skeleton.bones[i].scale.x = waistScaleW;
                    tmp.skeleton.bones[i].scale.z = waistScaleH;
                } else if (tmp.skeleton.bones[i].name === 'Pevis_S') { //臀部
                    tmp.skeleton.bones[i].scale.x = hiplinScaleW;
                    var hiplinScaleH1 = 0;
                    if (hiplinScaleH > 1.1 && hiplinScaleH <= 1.19) {
                        hiplinScaleH1 = hiplinScaleH * .9;
                    } else if (hiplinScaleH > 1.19) {
                        hiplinScaleH1 = hiplinScaleH * .84;
                    } else {
                        hiplinScaleH1 = 1;
                    }
                    tmp.skeleton.bones[i].scale.y = hiplinScaleH1;
                    tmp.skeleton.bones[i].scale.z = hiplinScaleH1;
                } else if (tmp.skeleton.bones[i].name === 'Leg_spacing') {

                    if (hiplinScaleW < 1.04) {

                        tmp.skeleton.bones[i].scale.x = 0.8;

                    }

                } else if (tmp.skeleton.bones[i].name === 'L_Leg_Up' || tmp.skeleton.bones[i].name === 'R_Leg_Up') { //腿部

                    if (hiplinScaleW < 1.04) {
                        tmp.skeleton.bones[i].scale.x = hiplinScaleW / .8;

                    } else  {
                        tmp.skeleton.bones[i].scale.x = hiplinScaleW;

                    }
                    console.log(legHeightScale);
                    if (legHeightScale > 1.12) {
                        tmp.skeleton.bones[i].scale.y = legHeightScale * .94; //大腿长
                    }
                    tmp.skeleton.bones[i].scale.z = hiplinScaleW;

                } else if (tmp.skeleton.bones[i].name === 'L_Foot' || tmp.skeleton.bones[i].name === 'R_Foot') {//脚部
                    //if (hiplinScaleW > 1.2) {
                    //    tmp.skeleton.bones[i].scale.x = .88;
                    //    tmp.skeleton.bones[i].scale.y = .88;
                    //    tmp.skeleton.bones[i].scale.z = .88;
                    //} else if (hiplinScaleW <= 1.2 && hiplinScaleW > 1) {
                    //    tmp.skeleton.bones[i].scale.x = .86;
                    //    tmp.skeleton.bones[i].scale.y = .86;
                    //    tmp.skeleton.bones[i].scale.z = .86;
                    //} else if (hiplinScaleW === 1) {
                    //    tmp.skeleton.bones[i].scale.x = .84;
                    //    tmp.skeleton.bones[i].scale.y = .84;
                    //    tmp.skeleton.bones[i].scale.z = .84;
                    //} else {
                    //    tmp.skeleton.bones[i].scale.x = .82;
                    //    tmp.skeleton.bones[i].scale.y = .82;
                    //    tmp.skeleton.bones[i].scale.z = .82;
                    //}
                    tmp.skeleton.bones[i].scale.x = 1 / hiplinScaleW * footScale;
                    tmp.skeleton.bones[i].scale.y = 1 / hiplinScaleW * footScale;
                    tmp.skeleton.bones[i].scale.z = 1 / hiplinScaleW * footScale;
                    //tmp.skeleton.bones[i].rotation.x = 1.16; //脚部旋转控制穿平跟鞋还是高跟鞋
                } else if (tmp.skeleton.bones[i].name === 'L_Hand' || tmp.skeleton.bones[i].name === 'R_Hand') {//手部
                    if (hiplinScaleW > 1.2) {
                        tmp.skeleton.bones[i].scale.x = .86;
                        tmp.skeleton.bones[i].scale.y = .86;
                        tmp.skeleton.bones[i].scale.z = .86;
                    } else if (hiplinScaleW <= 1.2 && hiplinScaleW > 1) {
                        tmp.skeleton.bones[i].scale.x = .84;
                        tmp.skeleton.bones[i].scale.y = .84;
                        tmp.skeleton.bones[i].scale.z = .84;
                    } else if (hiplinScaleW === 1) {
                        tmp.skeleton.bones[i].scale.x = .82;
                        tmp.skeleton.bones[i].scale.y = .82;
                        tmp.skeleton.bones[i].scale.z = .82;
                    } else {
                        tmp.skeleton.bones[i].scale.x = .8;
                        tmp.skeleton.bones[i].scale.y = .8;
                        tmp.skeleton.bones[i].scale.z = .8;
                    }
                }

                if (tmp.skeleton.bones[i].name === 'L_Arm_Shoulder' || tmp.skeleton.bones[i].name === 'R_Arm_Shoulder') {
                    var waistScaleW1 = 0;
                    if (waistScaleW > 1.1) {
                        waistScaleW1 = waistScaleW * .9;
                    } else {
                        waistScaleW1 = 1;
                    }

                    tmp.skeleton.bones[i].scale.x = waistScaleW1;
                    tmp.skeleton.bones[i].scale.y = legHeightScale;
                    tmp.skeleton.bones[i].scale.z = waistScaleW1;
                    console.log(waistScaleW1);
                    if (waistScaleW1 > 1.16) {
                        tmp.skeleton.bones[i].rotation.x = -.98;
                    }

                }
                if (tmp.skeleton.bones[i].name === 'L_Arm_Elbow') {

                    var waistScaleW1 = 0;
                    if (waistScaleW > 1.1) {
                        waistScaleW1 = waistScaleW * .9;
                    } else {
                        waistScaleW1 = 1;
                    }
                    console.log(waistScaleW1);
                    if (waistScaleW1 > 1.16) {
                        tmp.skeleton.bones[i].rotation.x = -.08;
                    }

                }

            }
        }
        //return headBias;
    }

};